package org.vidi.jms.common;

import com.rabbitmq.jms.admin.RMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.QueueConnection;

/**
 * @author vidi
 */
public class ConnectionUtils {
    public static QueueConnection getConnection() throws JMSException {
        return ((RMQConnectionFactory) getConnectionFactory()).createQueueConnection("admin", "admin");
    }

    public static ConnectionFactory getConnectionFactory() {
        RMQConnectionFactory rmqConnectionFactory = new RMQConnectionFactory() {
            private static final long serialVersionUID = 1L;

            @Override
            public Connection createConnection(String userName, String password) throws JMSException {
                this.setPort(5672);
                this.setHost("127.0.0.1");
                return super.createConnection("admin", "admin");
            }
        };
        rmqConnectionFactory.setQueueBrowserReadMax(0);
        return rmqConnectionFactory;
    }
}
